Computers / Programming / Projects / View-ViewModel base classes

Overview

These are the base classes and interfaces I've made to help with current and future WPF development.

ViewModel

These two classes form the basis for the others and ViewModels in general

ViewModelbase.cs

cs type icon
Type: Base class
Language: C#
ViewModelbase.cs

The base class for all ViewModels. This class implements the INotifyPropertyChanged interface and provides some conviencne methods for raising and setting properties.

ValidationViewModelBase.cs

cs type icon
Type: Base class
Language: C#
ValidationViewModelBase.cs

This class extends ViewModelBase and an implementation of IDataErrorInfo. It mainly provides a mecanisum for derived ViewModels to create validation rules. Classes that extent this base class can call AddValidation with to add a function that validates a specific property. The control associated with that property will pick up on this validation rule through the interface and display an error if the function doesn't return an empty string.

Window View/ViewModel

These files make up basic window type views. A window is the simplest type of control and is just displayed to the user. Every interactive program needs at least one window which acts as the central hub of the program.

WindowViewBase.cs

cs type icon
Type: View base
Language: C#
WindowViewBase.cs

This is an extension of the .NET window class which implements the desired functionality for how we want to use window views. It is created using a IWindowViewModel reference and sets up some handlers for various events that the view model can raise. This includes displaying a view, displaying a message box, and notifying the ViewModel when the view is loaded.

IWindowViewModel.cs

cs type icon
Type: View Model Interface
Language: C#
IWindowViewModel.cs

This is the interface implemented by WindowViewModelBase. The WindowViewBase makes all of its interaction with the ViewModel through this interface. This is done to make their interactions abstract and to keep either from knowing too much about the other. One of the main ideas of MVVM is separation of purpose and this interface is an attempt to create that separation. The view can call certain methods on the ViewModel through this interface and listen for various events to be raised by the ViewModel.

WindowViewModelBase.cs

cs type icon
Type: View Model Base
Language: C#
WindowViewModelBase.cs

This is the base ViewModel for all WindowViewBase controls. It provides a basic implementation of the IWindowViewModel interface and the INotifyPropertyChanged interface. It also has some helper methods used to raise the events defined in these interfaces. ViewModels for specific views extend this class to provide additional functionality. This class is not abstract to allow the default constructor for views to create a simple view model to be used in the designer.

RequestViewArgs.cs

cs type icon
Type: Event Args
Language: C#
RequestViewArgs.cs

This class defines the EventArgs class used with the Request View event. It contains a reference to a ViewModel which the ViewModel creates and passes to the View which creates a new View for that ViewModel using it.

RequestMsgBoxArgs.cs

cs type icon
Type: Event Args
Language: C#
RequestMsgBoxArgs.cs

This class defines the EventArgs class used with the Request Msg Box event. It contains properties used by the view to determine how the message box should be displayed. This includes the text to be displayed in the message box, the caption it should use, which buttons should be visible, and which icon should be displayed.

Dialog View/ViewModel

DialogViewBase.cs

cs type icon
Type: View base
Language: C#
DialogViewBase.cs

This is an extension to the WindowViewBase class which implements some additional functionality desired for dialogs. A dialog is a window that is shown in front of the other windows and blocks input to the other windows. The dialog class needs to listen for requests to close from its ViewModel and close accordingly.

IDialogViewModel.cs

cs type icon
Type: View Model Interface
Language: C#
IDialogViewModel.cs

This is the interface implemented by DialogViewModelBase. It serves the same purpose as the IWindowViewModel but with dialog specific additions.

DialogViewModelBase.cs

cs type icon
Type: View Model Base
Language: C#
DialogViewModelBase.cs

This is the base ViewModel for all DialogViewBase controls. It provides a basic implementation of the IDialogViewModel interface and has helper methods used to raise the events defined in it. It also inherits from WindowViewModelBase. ViewModels for specific dialog views extend this class to provide additional functionality. This class is not abstract to allow the default constructor for views to create a simple view model to be used in the designer.

RequestDialogCloseArgs.cs

cs type icon
Type: Event Args
Language: C#
RequestDialogCloseArgs.cs

This class defines the EventArgs class used with the Request Dialog Close event. It contains a single Boolean property which is which is used by the view to determine how it should close.